<?php

class Ms_Model_ForumTopic extends Zend_Db_Table_Abstract
{

    protected $_name = "forum_topics";
    
    protected $_dependentTables = array('Ms_Model_ForumPost');

    /**
     * 统计主题数
     * 
     * 统计某一版块下主题的数量
     * 
     * @param int $forum_id
     */
    public function countTopics($forum_id = null){
        $select = $this->select();
        $select->where('forum_id = ?', $forum_id);
        $topicNum = count($this->fetchAll($select));
        
        return $topicNum;
    }
    
    /**
     * 发表新主题
     *
     * 返回新增主题的id
     *
     * @param int $userid
     * @param int $forum_id
     * @param string $subject
     * @param string $post
     */
    public function createTopic($userid, $forum_id, $subject, $post){
        $row = $this->createRow();
        $row->time = time();
        $row->views = 0;
        $row->user_id = $userid;
        $row->forum_id = $forum_id;
        $row->subject = $subject;
        $row->post = $post;
        $row->save();
        if ($row){
            return $row->id;
        }
        else{
            return null;
        }
    }
    
    /**
     * 删除主题
     *
     * @param int $forum_id
     */
    public function latestTopic($forum_id = null){
        $select = $this->select();
        $select->where('forum_id = ?', $forum_id);
        $select->order('time DESC');
        $topic = $this->fetchRow($select);
        
        return $topic;
    }
    
    /**
     * 获取某一主题
     *
     * @param int $id
     */
    public function getTopic($id){
        $topic = $this->find($id)->current();
        if ($topic){
            return $topic;
        }
        else{
            return null;
        }
    }
    
    /**
     * 获取主题列表
     *
     * 根据条件获取某一版块主题或某用户的主题
     * 进行分页查询，返回分页结果
     *
     * @param int $fid
     * @param int $uid
     */
    public function getTopics($fid = null, $uid = null)
    {
        $select = $this->select()->setIntegrityCheck(false);// 可以跨表查询
        $select->from('forum_topics', '*');
        if($fid){
            $select->where('forum_id  = ?', $fid);
        }
        if($uid){
            $select->where('user_id = ?', $uid);
        }
        $select->order('id DESC');
        $select->join('core_users','forum_topics.user_id = core_users.id', 'username');
        
        $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
        
        return $adapter;
    }
    
    /**
     * 写入查看数
     *
     * 更新主题的查看数
     *
     * @param int $id
     */
    public function viewTopic($id){
        $post = $this->find($id)->current();
        $post->views = $post['views']+1;
        $post->save();
    }
    
    /**
     * 更新主题
     *
     * @param int $id
     * @param array $values
     */
    public function updateTopic($id,$values = array())
    {
        $row = $this ->find($id)->current();
        $row->subject = $values['subject'];
        $row ->post = $values['post'];
        $row ->save();
        if($row)
        {
            return true;
        }
        else{
            return false;
        }
    }
    
    /**
     * 删除主题
     *
     * @param int $id
     * @throws Zend_Exception
     */
    public function deleteTopic($id)
    {
        $row = $this->find($id)->current();
        if ($row){
            $row->delete();
        }
        else{
            throw new Zend_Exception('删除出错！没有找到该帖。');
        }
    }
}

